﻿using ZYCommon;
using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using Bifrost;

namespace TextEditor
{

    /// <summary>
    /// 文本域属性设置
    /// </summary>
    public class InputTagProvider : SmartTagProvider
    {
        public override void GetCommands(ArrayList myList)
        {
            ZYTextInput parent = base.Element.Parent as ZYTextInput;

            myList.Add(new SmartTagItem(this, 9, "&C  设置文本颜色..."));
            myList.Add(new SmartTagItem(this, 11, " 设置最小字符数"));
            myList.Add(new SmartTagItem(this, 12, " 设置最大字符数"));
            myList.Add(new SmartTagItem(this, 13, " 下划线"));
            if (!string.IsNullOrEmpty(parent.Name) && parent.Name.Contains("时间") || parent.Name.Contains("日期"))
            {
                myList.Add(new SmartTagItem(this, 14, "时间格式[" + parent.cFormat + "]"));
            }
            if (base.OwnerDocument.Us.InpatientInfo == null)
            {
                myList.Add(new SmartTagItem(this, 10, parent.CanDelete, " 锁定 文本域"));
                myList.Add(new SmartTagItem(this, 0, "&I  更改编号[ " + parent.ID + " ]为..."));
                myList.Add(new SmartTagItem(this, 1, "&N  更改名称[ " + parent.Name + " ]为..."));
            }
        }

        public override Point GetPos()
        {
            ZYTextInput parent = base.OwnerDocument.Content.CurrentElement.Parent as ZYTextInput;
            if (parent != null)
            {
                ZYTextElement lastElement = parent.GetLastElement();
                if (lastElement != null)
                {
                    return new Point(lastElement.Bounds.Right + 2, lastElement.Bounds.Bottom);
                }
                return Point.Empty;
            }
            return Point.Empty;
        }

        public override bool HandleCommand(SmartTagItem item)
        {
            ZYTextInput parent = base.OwnerDocument.Content.CurrentElement.Parent as ZYTextInput;
            if (parent != null)
            {
                switch (item.ID)
                {
                    case 0:
                        if (parent.Name != null && (parent.Name == "普通医师签名" || parent.Name == "上级医师签名" || parent.Name == "管床医师签名" || parent.Name == "转科上级医师签名"))
                        {
                            return false;
                        }
                        string strData = dlgInputBox.InputBox("请输入新的输入框的名称", "系统提示", parent.ID);
                        if (strData != null)
                        {
                            parent.ID = strData;
                            base.OwnerDocument.Modified = true;
                            return true;
                        }
                        break;
                    case 1:
                        if (parent.Name != null && (parent.Name == "普通医师签名" || parent.Name == "上级医师签名" || parent.Name == "管床医师签名"))
                        {
                            return false;
                        }
                        string str2 = dlgInputBox.InputBox("请输入新的输入框的名称", "系统提示", parent.Name);
                        if (!StringCommon.isBlankString(str2))
                        {
                            parent.Name = str2.Trim();
                            base.OwnerDocument.Modified = true;
                            return true;
                        }
                        break;
                    case 2:
                        A_Action actionByName = base.OwnerDocument.GetActionByName("setinputformat");
                        if ((actionByName != null) && actionByName.isEnable())
                        {
                            actionByName.UIExecute();
                        }
                        break;
                    case 3:
                        string data = item.Data;
                        ArrayList myList = base.OwnerDocument.CreateChars(item.Data, null);
                        ArrayList list2 = new ArrayList();
                        list2.AddRange(parent.ChildElements);
                        base.OwnerDocument.BeginContentChangeLog();
                        base.OwnerDocument.BeginUpdate();
                        parent.RemoveChildRange(list2);
                        parent.InsertRangeBefore(myList, null);
                        base.OwnerDocument.RefreshElements();
                        base.OwnerDocument.RefreshLine();
                        base.OwnerDocument.EndUpdate();
                        base.OwnerDocument.EndContentChangeLog();
                        if (myList.Count > 0)
                        {
                            base.OwnerDocument.Content.CurrentElement = (ZYTextElement)myList[0];
                        }
                        break;
                    case 4:
                        if (base.OwnerControl.KBTreeView != null)
                        {
                            base.OwnerControl.KBTreeView.SelectedKBList = ZYKBBuffer.Instance.GetKBList(parent.ListSource);
                            return true;
                        }
                        break;
                    case 5:
                        string str4 = dlgInputBox.InputBox("绑定到文档变量", "系统输入", parent.ValueSource);
                        if (str4 != null)
                        {
                            parent.ValueSource = str4.Trim();
                        }
                        break;
                    case 6:
                        parent.ValueSource = item.Data;
                        parent.Text = base.OwnerDocument.Variables.GetValue(item.Data);
                        base.OwnerDocument.RefreshElements();
                        base.OwnerDocument.RefreshLine();
                        base.OwnerDocument.UpdateView();
                        break;
                    case 7:
                        string str5 = dlgInputBox.InputBox("请输入新的显示名称", "系统输入", parent.DisplayName);
                        if (str5 != null)
                        {
                            parent.DisplayName = str5;
                            parent.RefreshSize();
                            base.OwnerDocument.RefreshLine();
                            base.OwnerDocument.UpdateView();
                            return true;
                        }
                        break;
                    case 8:
                        parent.KeyField = !parent.KeyField;
                        break;
                    case 9:
                        using (ColorDialog dialog = new ColorDialog())
                        {
                            dialog.Color = parent.TempForeColor;
                            if (dialog.ShowDialog() == DialogResult.OK)
                            {
                                parent.TempForeColor = dialog.Color;
                                parent.ForeColor = dialog.Color;
                                base.OwnerDocument.Refresh();
                            }
                        }
                        break;
                    case 10:
                        parent.CanDelete = !parent.CanDelete;
                        break;
                    case 11:
                        {
                            string strData1 = dlgInputBox.InputBox("输入框长度", "输入数字", parent.minLength.ToString());
                            if (strData1 != null)
                            {
                                if (Regex.Match(strData1.Trim(), @"^[0-9]+$").Success)
                                {
                                    int strLength = Convert.ToInt32(strData1.Trim());
                                    parent.minLength = strLength;
                                    base.OwnerDocument.RefreshLine();
                                }
                                else
                                {
                                    App.Msg("输入的不是数值类型！");
                                }
                            }
                        }
                        break;
                    case 12:
                        {
                            string strData2 = dlgInputBox.InputBox("输入框长度", "输入数字", parent.maxLength.ToString());
                            if (strData2 != null)
                            {
                                if (Regex.Match(strData2.Trim(), @"^[0-9]+$").Success)
                                {
                                    int strLength = Convert.ToInt32(strData2.Trim());
                                    parent.maxLength = strLength;
                                    base.OwnerDocument.RefreshLine();
                                }
                                else
                                {
                                    App.Msg("输入的不是数值类型！");
                                }
                            }
                        }
                        break;
                    case 13:
                        {
                            string strData0 = dlgInputBox.InputBox("样式：0，无下划线；1，下划线", "输入数字", parent.BorderStyle.ToString());
                            if (strData0 != null)
                            {
                                if (Regex.Match(strData0.Trim(), @"^[0-9]+$").Success)
                                {
                                    int strLength = Convert.ToInt32(strData0.Trim());
                                    parent.BorderStyle = strLength;
                                    base.OwnerDocument.RefreshLine();
                                }
                                else
                                {
                                    App.Msg("输入的不是数值类型！");
                                }
                            }
                        }
                        break;
                    case 14:
                        string strData4 = dlgInputBox.InputBox("时间格式设置", "输入格式", parent.cFormat);
                        if (strData4 != null)
                        {
                            parent.cFormat = strData4;
                        }
                        break;
                }
            }
            return false;
        }

        public override bool isEnable()
        {
            ZYTextElement currentElement = base.OwnerDocument.Content.CurrentElement;
            if (currentElement != null && base.OwnerDocument.Content.SelectLength == 0)
            {
                if (currentElement.Parent is ZYTextInput)
                {
                    ZYTextInput input = (ZYTextInput)currentElement.Parent;
                    if (base.OwnerDocument.Us.InpatientInfo != null && (input.Name == "普通医师签名" || input.Name == "管床医师签名" || input.Name == "上级医师签名" || input.Name == "普通医师日期" || input.Name == "上级医师日期"))
                    {
                        return false;
                    }
                    return true;
                }
            }
            return false;
        }

        public override int ImageIndex
        {
            get
            {
                return 12;
            }
        }
    }
}

